<?php
namespace Home\Controller;
use Think\Controller;
use Common\Service\LogSercvice;
use Common\Service\UserSercvice;

class UserController extends Controller{

	public function login(){
		if(IS_POST){
			$name = I('post.name','');
			$password = I('post.password','');
			if($name!=''&& $password!=''){
				$user= D('User','Service');
				$info=$user->GetUserInfoByName($name);
				$password=md5($password);
				if($password==$info['password']){
					session('home.userinfo',$info);
					$log= D('Log','Service');
					$log->AddLog();
					$this->assign('title','主页');
					$this->redirect('/Home/Index');
				}
			}
 		}
		$this->assign('title','欢迎登陆平台');
		$this->display();
	}

	public function register(){
		
		$this->assign("title","注册");
		$this->display();
	}
	public function loginout(){
		session('home.userinfo',null);
		if(empty($_SESSION['home']['userinfo'])){
			echo '退出成功';
		}
	}

	public function doregister(){
		if($_POST){
			$username = I('post.username');
			$email = I('post.email');
			$user=D("User","Service");
			$resUsername = $user->GetUserByUserName($username);
			if($resUsername){
				$this->redirect('register',array('msgTitle'=>'错误','msgContext'=> '用户名已被使用'));
				return ;
			}
			$resEmail = $user->GetUserByEmail($email);
			if($resEmail){
				$this->redirect('register',array('msgTitle'=>'错误','msgContext'=> 'Email已被使用'));
				return ;
			}
			$res=$user->AddUser();
			if($res){
				$this->redirect('login');
			}else{
				$this->redirect('register',array('msgTitle'=>'错误','msgContext'=> '注册出错'));
			}
		}
	}

}